package com.forhappy.light3ds8.ui;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.forhappy.light3ds8.R;
import com.forhappy.light3ds8.arm.bluetooth.ArmMgr;
import com.forhappy.light3ds8.db.LedDBContentProvider;
import com.forhappy.light3ds8.db.LedDBHelper;
import com.forhappy.light3ds8.tools.DatasUtils;

public class Pg2_send_to_arm extends LinearLayout 
		implements View.OnClickListener, IRealDatasChangeCallBack{
	private static final String TAG = "Pg2_send_to_arm";
	EditText edDatas = null;
	Handler handler = null;
	public static Pg2_send_to_arm g_Pg2_send_to_arm = null;
	public Pg2_send_to_arm(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.d(TAG, "enter Pg2_send_to_arm()");
		initHandler();
		g_Pg2_send_to_arm = this;
	}
	public static void notifyRealDatasChange(int idxRealDatas){
		if(null != g_Pg2_send_to_arm){
			g_Pg2_send_to_arm.onRealDatasChange(idxRealDatas);
		}
	}

	@Override
	protected void onFinishInflate() {
		Log.d(TAG, "enter onFinishInflate()");
		super.onFinishInflate();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.d(TAG, "enter onAttachedToWindow()");
		super.onAttachedToWindow();
		Button bSendToArm = (Button) findViewById(R.id.bSendToArm);
		bSendToArm.setOnClickListener(this);

		edDatas = (EditText) findViewById(R.id.eSendDatas);
	}
	@Override
	protected void onDetachedFromWindow() {
		Log.d(TAG, "enter onDetachedFromWindow()");
		super.onDetachedFromWindow();
	}
	@Override
	public void onClick(View view) {
		Log.d(TAG, "enter onClick(view:" + view + ")");
		if(view.getId() == R.id.bSendToArm){
			String datas = edDatas.getText().toString();
			ArmMgr.sendDatas(DatasUtils.hexStrToBytes(datas));
		}
	}


	public final int MSG_LOAD_REAL_DATAS = 100;
	public final int MSG_SET_REAL_DATAS = 101;
	private void initHandler(){
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MSG_LOAD_REAL_DATAS:
					int idx  = (Integer) msg.obj;
					LoadRealDatasInThread(idx);
					break;
				case MSG_SET_REAL_DATAS:
					String realDatas = (String) msg.obj;
					EditText ed = (EditText) findViewById(R.id.eSendDatas);
					ed.setText(realDatas);
					ArmMgr.sendDatas(DatasUtils.hexStrToBytes(realDatas));
				default:
					break;
				}
			}
		};
	}
	@Override
	public void onRealDatasChange(final int idxRealDatas) {
		handler.removeMessages(MSG_LOAD_REAL_DATAS);
		Message msg = handler.obtainMessage(MSG_LOAD_REAL_DATAS, idxRealDatas);
		handler.sendMessageDelayed(msg, 1000);
	}
	
	private void LoadRealDatasInThread(final int idxRealDatas){
		new Thread(){
			@Override
			public void run() {
				ContentResolver cr = getContext().getContentResolver();
				Cursor rs= cr.query(LedDBContentProvider.URI_DATA_REAL,
					new String[]{LedDBHelper.ITEM_DATAS}, 
					" id = ?", new String[]{String.valueOf(idxRealDatas)}, null);
				if(null == rs || rs.isAfterLast()){
					Log.e(TAG, "in load real datas, no result is return, idx:" + idxRealDatas);
					return;
				}
				
				if(!rs.moveToNext()){
					Log.e(TAG, "maybe no datas found for real datas! idx:" + idxRealDatas);
					return;
				}
				String realDatas = rs.getString(0);
				rs.close();
				Message msg = handler.obtainMessage();
				msg.what = MSG_SET_REAL_DATAS;
				msg.obj = realDatas;
				handler.sendMessage(msg);
			}
		}.start();
	}
}
interface IRealDatasChangeCallBack{
	void onRealDatasChange(int idxRealDatas);
}
